package com.ruoyi.find.controller;

import com.ruoyi.find.domain.ChatRequest;
import com.ruoyi.find.domain.ChatResponse;
import com.ruoyi.find.service.AiService;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/ai")
public class ChatController {

    private final AiService aiService;

    public ChatController(AiService aiService) {
        this.aiService = aiService;
    }

    @PostMapping("/api/chat")
    public ChatResponse chat(@RequestBody ChatRequest request) {
        ChatResponse response = new ChatResponse();
        try {
            String answer = aiService.ask(request.getQuestion());
            response.setSuccess(true);
            response.setAnswer(answer);
        } catch (Exception e) {
            response.setSuccess(false);
            response.setErrorMsg("调用 AI 失败：" + e.getMessage());
        }
        return response;
    }
}